<template>
  <div class="setting-body">
    <el-form ref="form" :model="form" size="small" label-width="120px">
      <div class="custom-tips">
        基础设置
      </div>
      <el-row :gutter="20" style="margin-top: 10px">
        <el-col :span="24">
          <el-form-item
            label="地址"
            prop="url"
            :rules="[{ required: true, message: '请输入地址', trigger: 'blur' }]"
          >
            <el-input v-model="form.url" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="custom-button">
      <el-button type="text" @click="close">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save('form')">确认</el-button>
    </div>
  </div>
</template>
<script>
import { updateObject } from '@/api/portal/manager/columnTemp';
export default {
  name: 'Portal4PcDividerV1',
  props: {
    data: {
      type: Object,
      default: function() {
        return {};
      }
    }
  },
  data() {
    return {
      loading: false,
      form: {
        url: ''
      }
    };
  },
  watch: {
    data: {
      handler(newName, oldName) {
        if (undefined !== newName) {
          if (undefined !== newName.pzx && newName.pzx !== '' && newName.pzx !== null) {
            this.form = JSON.parse(newName.pzx);
          }
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  methods: {
    save() {
      this.loading = true;
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.data.pzx = JSON.stringify(this.form);
          updateObject(this.data).then(res => {
            this.close();
          }).catch(() => {
            this.loading = false;
          });
        } else {
          this.loading = false;
        }
      });
    },
    close() {
      this.loading = false;
      Object.assign(this.$data.form, this.$options.data.call(this).form);
      this.dialogFormVisible = false;
      this.$emit('fetch-data');
    }
  }
};
</script>
<style scoped>
.setting-body {
  width: 100%;
}
.custom-tips {
  width: 100%;
  min-height: 40px;
  line-height: 40px;
  text-align: left;
  padding: 8px 16px;
  background-color: #ecf8ff;
  border-radius: 4px;
  border-left: 5px solid #50bfff;
}

.custom-button {
  width: 100%;
  height: 40px;
  line-height: 40px;
  text-align: right;
}
</style>
